package com.zj.juc.study.interrupted;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @author 自己的名字
 * @version 1.0.0
 * @ClassName InterruptedAtomicBooleanDemo
 * @Description 运用AtomicBoolean打断线程
 * @createTime 2024年12月26日 21:03
 */
public class InterruptedAtomicBooleanDemo {

    static AtomicBoolean isStop = new AtomicBoolean(false);

    //原子操作类
    public static void main(String[] args) {
        new Thread(()->{
            while (true){
                if (isStop.get()){
                    System.out.println(Thread.currentThread().getName()+"modify AtomicBoolean to true");
                    break;
                }
                System.out.println("hello atomic");
            }
        },"t1").start();
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new Thread(()->{
            isStop.set(true);
        },"t2").start();
    }
}
